using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public static class ExtensionMethods
	{
		private enum VisibleSearchMode
		{
			Before,
			During,
			After
		}
		private static List<Action> workItems;
		public static T GetRootPage<T>(this FrameworkElement element) where T : DependencyObject
		{
			for (FrameworkElement visualParent = element.GetVisualParent(); visualParent != null; visualParent = visualParent.GetVisualParent())
			{
				if (visualParent is T)
				{
					return visualParent as T;
				}
			}
			return default(T);
		}
		public static Point GetRelativePosition(this UIElement element, UIElement other)
		{
			return element.TransformToVisual(other).Transform(new Point(0.0, 0.0));
		}
		public static FrameworkElement FindVisualChild(this FrameworkElement root, string name)
		{
			FrameworkElement frameworkElement = root.FindName(name) as FrameworkElement;
			if (frameworkElement != null)
			{
				return frameworkElement;
			}
			FrameworkElement result;
			using (IEnumerator<FrameworkElement> enumerator = root.GetVisualDescendents().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FrameworkElement current = enumerator.get_Current();
					frameworkElement = (current.FindName(name) as FrameworkElement);
					if (frameworkElement != null)
					{
						result = frameworkElement;
						return result;
					}
				}
				goto IL_52;
			}
			return result;
			IL_52:
			return null;
		}
		public static FrameworkElement GetVisualChild(this FrameworkElement node, int index)
		{
			try
			{
				return VisualTreeHelper.GetChild(node, index) as FrameworkElement;
			}
			catch (Exception)
			{
			}
			return null;
		}
		public static IEnumerable<FrameworkElement> GetVisualChildren(this FrameworkElement root)
		{
			ExtensionMethods.<GetVisualChildren>d__0 <GetVisualChildren>d__ = new ExtensionMethods.<GetVisualChildren>d__0(-2);
			<GetVisualChildren>d__.<>3__root = root;
			return <GetVisualChildren>d__;
		}
		public static IEnumerable<T> PrependWith<T>(this IEnumerable<T> list, T head)
		{
			ExtensionMethods.<PrependWith>d__4<T> <PrependWith>d__ = new ExtensionMethods.<PrependWith>d__4<T>(-2);
			<PrependWith>d__.<>3__list = list;
			<PrependWith>d__.<>3__head = head;
			return <PrependWith>d__;
		}
		public static VisualStateGroup GetVisualStateGroup(this FrameworkElement root, string groupName)
		{
			IEnumerable<FrameworkElement> enumerable = root.GetVisualAncestors().PrependWith(root);
			VisualStateGroup result;
			using (IEnumerator<FrameworkElement> enumerator = enumerable.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FrameworkElement current = enumerator.get_Current();
					IList visualStateGroups = VisualStateManager.GetVisualStateGroups(current);
					IEnumerator enumerator2 = visualStateGroups.GetEnumerator();
					try
					{
						while (enumerator2.MoveNext())
						{
							object current2 = enumerator2.get_Current();
							VisualStateGroup visualStateGroup = current2 as VisualStateGroup;
							if (visualStateGroup != null && visualStateGroup.get_Name() == groupName)
							{
								result = visualStateGroup;
								return result;
							}
						}
					}
					finally
					{
						IDisposable disposable = enumerator2 as IDisposable;
						if (disposable != null)
						{
							disposable.Dispose();
						}
					}
				}
				goto IL_92;
			}
			return result;
			IL_92:
			return null;
		}
		public static bool TestVisibility(this FrameworkElement item, FrameworkElement viewport, Orientation orientation, bool wantVisible)
		{
			bool result = false;
			try
			{
				GeneralTransform generalTransform = item.TransformToVisual(viewport);
				Point point = generalTransform.Transform(new Point(0.0, 0.0));
				Point point2 = generalTransform.Transform(new Point(item.get_ActualWidth(), item.get_ActualHeight()));
				double num;
				double num2;
				double num3;
				double num4;
				if (orientation == null)
				{
					num = point.get_Y();
					num2 = point2.get_Y();
					num3 = 0.0;
					num4 = Math.Min(viewport.get_ActualHeight(), double.IsNaN(viewport.get_Height()) ? double.PositiveInfinity : viewport.get_Height());
				}
				else
				{
					num = point.get_X();
					num2 = point2.get_X();
					num3 = 0.0;
					num4 = Math.Min(viewport.get_ActualWidth(), double.IsNaN(viewport.get_Width()) ? double.PositiveInfinity : viewport.get_Width());
				}
				result = wantVisible;
				if (num >= num4 || num2 <= num3)
				{
					result = !wantVisible;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}
		public static IEnumerable<T> GetVisibleItems<T>(this IEnumerable<T> items, FrameworkElement viewport, Orientation orientation) where T : FrameworkElement
		{
			IEnumerable<T> enumerable = Enumerable.SkipWhile<T>(items, (T item) => item.TestVisibility(viewport, orientation, false));
			return Enumerable.TakeWhile<T>(enumerable, (T item) => item.TestVisibility(viewport, orientation, true));
		}
		public static void GetVisibleItems<T>(this IEnumerable<T> items, FrameworkElement viewport, Orientation orientation, out List<T> beforeItems, out List<T> visibleItems, out List<T> afterItems) where T : FrameworkElement
		{
			beforeItems = new List<T>();
			visibleItems = new List<T>();
			afterItems = new List<T>();
			ExtensionMethods.VisibleSearchMode visibleSearchMode = ExtensionMethods.VisibleSearchMode.Before;
			using (IEnumerator<T> enumerator = items.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					T current = enumerator.get_Current();
					switch (visibleSearchMode)
					{
					case ExtensionMethods.VisibleSearchMode.Before:
						if (current.TestVisibility(viewport, orientation, false))
						{
							beforeItems.Add(current);
						}
						else
						{
							visibleItems.Add(current);
							visibleSearchMode = ExtensionMethods.VisibleSearchMode.During;
						}
						break;
					case ExtensionMethods.VisibleSearchMode.During:
						if (current.TestVisibility(viewport, orientation, true))
						{
							visibleItems.Add(current);
						}
						else
						{
							afterItems.Add(current);
							visibleSearchMode = ExtensionMethods.VisibleSearchMode.After;
						}
						break;
					default:
						afterItems.Add(current);
						break;
					}
				}
			}
		}
		public static IEnumerable<FrameworkElement> GetVisualDescendents(this FrameworkElement root)
		{
			ExtensionMethods.<GetVisualDescendents>d__e <GetVisualDescendents>d__e = new ExtensionMethods.<GetVisualDescendents>d__e(-2);
			<GetVisualDescendents>d__e.<>3__root = root;
			return <GetVisualDescendents>d__e;
		}
		public static IEnumerable<T> GetVisualDescendents<T>(this FrameworkElement root, bool allAtSameLevel) where T : FrameworkElement
		{
			bool flag = false;
			IEnumerator<FrameworkElement> enumerator = root.GetVisualDescendents().GetEnumerator();
			while (true)
			{
				FrameworkElement current;
				try
				{
					IL_82:
					while (enumerator.MoveNext())
					{
						current = enumerator.get_Current();
						if (current is T)
						{
							goto IL_97;
						}
						if (flag && allAtSameLevel)
						{
							goto IL_BF;
						}
					}
					break;
					IL_4B:
					goto IL_82;
				}
				finally
				{
					if (enumerator != null)
					{
						enumerator.Dispose();
					}
				}
				break;
				IL_97:
				flag = true;
				yield return current as T;
				goto IL_4B;
			}
			yield break;
			IL_BF:
			yield break;
		}
		[Conditional("DEBUG")]
		public static void PrintDescendentsTree(this FrameworkElement root)
		{
			List<string> list = new List<string>();
			using (List<string>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string arg_17_0 = enumerator.get_Current();
				}
			}
		}
		[Conditional("DEBUG")]
		private static void GetChildTree(this FrameworkElement root, string prefix, string addPrefix, List<string> results)
		{
			string text;
			if (string.IsNullOrEmpty(root.get_Name()))
			{
				text = "[Anon]";
			}
			else
			{
				text = root.get_Name();
			}
			text = text + " " + root.GetType().get_Name();
			results.Add(prefix + text);
			using (IEnumerator<FrameworkElement> enumerator = root.GetVisualChildren().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FrameworkElement arg_5B_0 = enumerator.get_Current();
				}
			}
		}
		[Conditional("DEBUG")]
		public static void PrintAncestorTree(this FrameworkElement node)
		{
			List<string> list = new List<string>();
			string text = "";
			using (List<string>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string arg_1D_0 = enumerator.get_Current();
					text += "  ";
				}
			}
		}
		[Conditional("DEBUG")]
		private static void GetAncestorVisualTree(this FrameworkElement node, List<string> children)
		{
			string text = string.IsNullOrEmpty(node.get_Name()) ? "[Anon]" : node.get_Name();
			string text2 = text + ": " + node.GetType().get_Name();
			children.Insert(0, text2);
			node.GetVisualParent();
		}
		public static double GetVerticalScrollOffset(this ListBox list)
		{
			ScrollViewer scrollViewer = list.FindVisualChild("ScrollViewer") as ScrollViewer;
			return scrollViewer.get_VerticalOffset();
		}
		public static double GetHorizontalScrollOffset(this ListBox list)
		{
			ScrollViewer scrollViewer = list.FindVisualChild("ScrollViewer") as ScrollViewer;
			return scrollViewer.get_HorizontalOffset();
		}
		private static bool Internal_SetVerticalScrollOffset(this ListBox list, double offset)
		{
			ScrollViewer scrollViewer = list.FindVisualChild("ScrollViewer") as ScrollViewer;
			if (scrollViewer != null)
			{
				scrollViewer.ScrollToVerticalOffset(offset);
				if (list is ISupportOffsetChanges)
				{
					(list as ISupportOffsetChanges).VerticalOffsetChanged(offset);
				}
				return true;
			}
			return false;
		}
		private static bool Internal_SetHorizontalScrollOffset(this ListBox list, double offset)
		{
			ScrollViewer scrollViewer = list.FindVisualChild("ScrollViewer") as ScrollViewer;
			if (scrollViewer != null)
			{
				scrollViewer.ScrollToHorizontalOffset(offset);
				if (list is ISupportOffsetChanges)
				{
					(list as ISupportOffsetChanges).HorizontalOffsetChanged(offset);
				}
				return true;
			}
			return false;
		}
		public static void SetVerticalScrollOffset(this ListBox list, double offset)
		{
			if (list.Internal_SetVerticalScrollOffset(offset))
			{
				return;
			}
			ExtensionMethods.ScheduleOnNextRender(delegate
			{
				list.Internal_SetVerticalScrollOffset(offset);
			});
		}
		public static void SetHorizontalScrollOffset(this ListBox list, double offset)
		{
			if (list.Internal_SetHorizontalScrollOffset(offset))
			{
				return;
			}
			ExtensionMethods.ScheduleOnNextRender(delegate
			{
				list.Internal_SetHorizontalScrollOffset(offset);
			});
		}
		public static void ScheduleOnNextRender(Action action)
		{
			if (ExtensionMethods.workItems == null)
			{
				ExtensionMethods.workItems = new List<Action>();
				CompositionTarget.add_Rendering(new EventHandler(ExtensionMethods.DoWorkOnRender));
			}
			ExtensionMethods.workItems.Add(action);
		}
		private static void DoWorkOnRender(object sender, EventArgs e)
		{
			CompositionTarget.remove_Rendering(new EventHandler(ExtensionMethods.DoWorkOnRender));
			List<Action> list = ExtensionMethods.workItems;
			ExtensionMethods.workItems = null;
			using (List<Action>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Action current = enumerator.get_Current();
					try
					{
						current.Invoke();
					}
					catch (Exception)
					{
						if (Debugger.get_IsAttached())
						{
							Debugger.Break();
						}
					}
				}
			}
		}
	}
}
